﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineGameClient.ViewModel;

namespace OnlineGameClient.View
{
    public partial class GameWindow : ChildWindow
    {
        public GameWindow()
        {
            InitializeComponent();
            this.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(GameWindow_Closing);
        }

        void GameWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var vm = this.DataContext as GameRoomViewModel;
            var state = vm.CurrentGameRoom.GameState;
            if (state == OnlineGameLibrary.Contract.GameState.Running)
            {
                var ret = MessageBox.Show("The game is still ongoing, are you sure to quit?", "warining", MessageBoxButton.OKCancel);
                if (ret != MessageBoxResult.OK)
                    e.Cancel = true;
                else
                {
                    vm.CurrentGame.DisposeGame();
                    vm.CurrentGame = null;
                    vm.LeaveGameRoomCommand.Execute(null);
                }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

